package com.sjz.han.industry.activites;

import android.arch.lifecycle.Observer;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sjz.han.industry.R;
import com.sjz.han.industry.adapters.WaitJhMemberAdapter;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.GongLueEntity;
import com.sjz.han.industry.entities.WaitJhMemberEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.LiveDataBus;
import com.sjz.han.industry.utils.PicUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 待激活成员列表
 */
public class WaitJhMemberActivity extends BaseActivity implements View.OnClickListener{

    private RefreshLayout smartrefresh;
    private RecyclerView action_recy;
    private WaitJhMemberAdapter memberAdapter;
    private List<WaitJhMemberEntity.WaitJhMemberData> memberDatas;
    private int pageNum = 1;
    private String title="";
    private boolean isFirst = true;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_waitjhmember;
    }

    @Override
    protected void initDatas() {
        memberDatas=new ArrayList<>();
      getDjh(String.valueOf(pageNum));

    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("待激活成员");

        smartrefresh = (RefreshLayout) this.findViewById(R.id.common_smartrefresh);
        action_recy = (RecyclerView) this.findViewById(R.id.common_recy);
        action_recy.setLayoutManager(new LinearLayoutManager(this));
        memberAdapter= new WaitJhMemberAdapter(this);
        action_recy.setAdapter(memberAdapter);
        //注册订阅者获取门店信息
        LiveDataBus.get()
                .with("Userid", String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        for(WaitJhMemberEntity.WaitJhMemberData entity:memberDatas){
                            if(entity.userId.equals(s)){
                                memberDatas.remove(entity);
                                memberAdapter.setAdapterDatas(memberDatas);
                                break;
                            }
                        }
                    }
                });
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        smartrefresh.setOnRefreshListener(headResh);
        smartrefresh.setOnLoadmoreListener(footerResh);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
        }
    }
    public OnRefreshListener headResh = new OnRefreshListener() {
        @Override
        public void onRefresh(RefreshLayout refreshlayout) {
            isFirst = false;
            smartrefresh.resetNoMoreData();
            pageNum = 1;
            getDjh(String.valueOf(pageNum));
        }
    };

    public OnLoadmoreListener footerResh = new OnLoadmoreListener() {
        @Override
        public void onLoadmore(RefreshLayout refreshlayout) {
            isFirst = false;
            if (pageNum == 1 && memberDatas.size() < ReqestUrl.PAGESIZE) {
                smartrefresh.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
            } else {
                pageNum++;
                getDjh(String.valueOf(pageNum));
            }
        }
    };
    @Override
    protected void onRetry() {

    }
    //获取
    public void getDjh(String page){
        if(isFirst){
            showLoading();
        }
        OkHttpUtils
                .post()
                .url(ReqestUrl.DJH_HYLIST_URL)
                .addParams("token",shareUtils.getToken())
                .addParams("pageSize",String.valueOf(ReqestUrl.PAGESIZE))
                .addParams("page",page)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if(!isFirst){
                    smartrefresh.finishLoadmore();
                    smartrefresh.finishRefresh();
                }
                showFaild();
            }
            @Override
            public void onResponse(String response, int id) {

                if(ReqestUrl.rebacRequestJson(response)!=null){
                    isFirst=false;
                    WaitJhMemberEntity entity= JsonUtils.getObject(response,WaitJhMemberEntity.class);
                    if(entity!=null&&entity.code==0&&entity.data!=null){
                        showSuccess();
                        setAdapterDatas(entity.data);
                    }else{
                        smartrefresh.finishRefresh();
                        smartrefresh.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                        if(pageNum==1||memberDatas.size()==0){
                            showEmptyData();
                        }
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    public void setAdapterDatas(List<WaitJhMemberEntity.WaitJhMemberData> datas){
        smartrefresh.finishLoadmore();
        smartrefresh.finishRefresh();
        if(pageNum==1){
            memberDatas.clear();
        }
        for(WaitJhMemberEntity.WaitJhMemberData entity:datas){
            memberDatas.add(entity);
        }
        memberAdapter.setAdapterDatas(memberDatas);
    }

}
